package com.personal.portal.admin.controller;

import com.personal.portal.common.entity.Result;
import com.personal.portal.admin.entity.Skill;
import com.personal.portal.admin.service.SkillService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 技能控制器
 * 
 * @author zhanghui
 * @version 1.0.0
 * @since 2025-11-10
 */
@Tag(name = "技能", description = "技能信息管理接口")
@RestController
@RequestMapping("/admin/skill")
@CrossOrigin(origins = "*", allowedHeaders = "*")
public class SkillController {

    @Resource
    private SkillService skillService;

    @Operation(summary = "根据ID查询技能", description = "根据技能ID查询详细信息")
    @GetMapping("/{id}")
    public Result<Skill> queryById(
            @Parameter(description = "技能ID", required = true) 
            @PathVariable Long id) {
        try {
            Skill skill = skillService.queryById(id);
            if (skill != null) {
                return Result.success(skill);
            } else {
                return Result.error("技能信息不存在");
            }
        } catch (Exception e) {
            return Result.error("查询技能信息失败: " + e.getMessage());
        }
    }

    @Operation(summary = "根据条件查询技能列表", description = "根据技能名称、分类等条件查询技能信息")
    @PostMapping("/list")
    public Result<List<Skill>> queryList(
            @Parameter(description = "技能查询条件") 
            @RequestBody Skill skill) {
        try {
            List<Skill> skillList = skillService.queryList(skill);
            return Result.success(skillList);
        } catch (Exception e) {
            return Result.error("查询技能列表失败: " + e.getMessage());
        }
    }

    @Operation(summary = "分页查询技能", description = "支持分页和条件查询的技能信息")
    @PostMapping("/page")
    public Result<Result.PageResult<Skill>> queryPage(
            @Parameter(description = "页码，从1开始", example = "1") 
            @RequestParam(defaultValue = "1") int pageNum,
            @Parameter(description = "每页数量", example = "10") 
            @RequestParam(defaultValue = "10") int pageSize,
            @Parameter(description = "技能查询条件") 
            @RequestBody Skill skill) {
        try {
            Result.PageResult<Skill> pageResult = skillService.queryPage(pageNum, pageSize, skill);
            return Result.success(pageResult);
        } catch (Exception e) {
            return Result.error("分页查询技能失败: " + e.getMessage());
        }
    }

    @Operation(summary = "创建技能", description = "新增一条技能信息")
    @PostMapping
    public Result<Long> create(
            @Parameter(description = "技能信息", required = true) 
            @RequestBody Skill skill) {
        try {
            int result = skillService.insert(skill);
            if (result > 0) {
                return Result.success("创建技能成功", skill.getId());
            } else {
                return Result.error("创建技能失败");
            }
        } catch (Exception e) {
            return Result.error("创建技能失败: " + e.getMessage());
        }
    }

    @Operation(summary = "更新技能", description = "更新指定ID的技能信息")
    @PutMapping("/{id}")
    public Result<Long> update(
            @Parameter(description = "技能ID", required = true) 
            @PathVariable Long id,
            @Parameter(description = "技能信息", required = true) 
            @RequestBody Skill skill) {
        try {
            skill.setId(id);
            int result = skillService.update(skill);
            if (result > 0) {
                return Result.success("更新技能成功", id);
            } else {
                return Result.error("更新技能失败");
            }
        } catch (Exception e) {
            return Result.error("更新技能失败: " + e.getMessage());
        }
    }

    @Operation(summary = "删除技能", description = "根据ID删除指定的技能信息")
    @DeleteMapping("/{id}")
    public Result<Long> delete(
            @Parameter(description = "技能ID", required = true) 
            @PathVariable Long id) {
        try {
            int result = skillService.deleteById(id);
            if (result > 0) {
                return Result.success("删除技能成功", id);
            } else {
                return Result.error("删除技能失败");
            }
        } catch (Exception e) {
            return Result.error("删除技能失败: " + e.getMessage());
        }
    }

    @Operation(summary = "批量删除技能", description = "根据ID列表批量删除技能信息")
    @DeleteMapping("/batch")
    public Result<List<Long>> deleteBatch(
            @Parameter(description = "技能ID列表", required = true) 
            @RequestBody List<Long> ids) {
        try {
            List<Long> successIds = skillService.deleteBatch(ids);
            return Result.success(
                String.format("成功删除 %d 条记录", successIds.size()), successIds);
        } catch (Exception e) {
            return Result.error("批量删除技能失败: " + e.getMessage());
        }
    }
}